package chat4_tedu.mychatClient.ui;

import chat4_tedu.mychatClient.Client;
import chat4_tedu.mychatClient.vo.Request_Command;
import chat4_tedu.mychatServer.utils.IOUtil;
import javax.swing.*;
import java.awt.*;
import java.io.InputStream;
import java.io.OutputStream;

//登录界面
public class LoginWindow {

	private JFrame jFrame = null; 
	private JPanel jContentPane = null;
	private JLabel jLabelLoginUserName = null;
	private JLabel jLabelLoginPassword = null;
	private JTextField jTextFieldLoginUserName = null;
	private JPasswordField passwordField = null;
	public JButton jButtonLogin = null;
	public JButton jButtonReg = null;
	private Client clientmain;
	private InputStream in;
	private OutputStream out;

	public LoginWindow(Client clientmain){
		//LinkInfo表示客户端需要的所有信息
		//(用来操作客户端中的任意一个界面<从LinkInfo中获取任意一个界面，再控制它隐藏或显示>)
		this.clientmain = clientmain;
		//连接服务器端
		if(clientmain !=null){//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//this.clientmain.initSocket();
		}
		getJFrame();
	}

	/**
	 * 用代码关闭登录窗口窗口(并将LinkInfo中的登录窗口设置为null)
	 */
	public void closeFrame(){
		this.jFrame.dispose();
		this.clientmain.setLoginWin(null);
	}

	//创建登录窗口的JFrame对象
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setSize(new Dimension(389, 274));
			jFrame.setTitle("登陆");
			jFrame.setResizable(false);

			jContentPane = new JPanel();

			jLabelLoginUserName = new JLabel();
			jLabelLoginUserName.setBounds(new Rectangle(18, 53, 335, 38));
			jLabelLoginUserName.setFont(new Font("Dialog", Font.BOLD, 14));
			jLabelLoginUserName.setText("  账    号：");

			jLabelLoginPassword = new JLabel();
			jLabelLoginPassword.setBounds(new Rectangle(18, 107, 335, 38));
			jLabelLoginPassword.setFont(new Font("Dialog", Font.BOLD, 14));
			jLabelLoginPassword.setText("  密    码：");


			jContentPane.setLayout(null);
			jContentPane.add(jLabelLoginUserName, null);
			jContentPane.add(jLabelLoginPassword, null);


			if (jTextFieldLoginUserName == null) {
				jTextFieldLoginUserName = new JTextField();
				jTextFieldLoginUserName.setBounds(new Rectangle(84, 56, 266, 33));
			}


			jContentPane.add(jTextFieldLoginUserName, null);

			if (passwordField == null) {
				passwordField = new JPasswordField();
				passwordField.setBounds(new Rectangle(84, 111, 266, 33));
			}
			jContentPane.add(passwordField, null);


			if (jButtonLogin == null) {
				jButtonLogin = new JButton();
				jButtonLogin.setBounds(new Rectangle(143, 162, 120, 41));
				jButtonLogin.setText("登陆");
				jButtonLogin.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						//当点击了登陆按钮时，调用登陆方法
						doLogin();
						//如果获取来自服务器端信息的线程getServerMsgThread没有启动，则启动getServerMsgThread线程
					}
				});
			}
			jContentPane.add(jButtonLogin, null);
//			jContentPane.add(getJButtonLogin(), null);


			if (jButtonReg == null) {
				jButtonReg = new JButton();
				jButtonReg.setBounds(new Rectangle(295, 214, 82, 28));
				jButtonReg.setText("去注册");
				jButtonReg.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						//如果点击了去注册按钮
						//关闭登陆框
						closeFrame();
						//显示注册框
						clientmain.setReg(new RegWindow(clientmain));
					}
				});
			}
			jContentPane.add(jButtonReg, null);

			jFrame.setContentPane(jContentPane);
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jFrame.setLocation(400,200);
			jFrame.setVisible(true);
		}
		return jFrame;
	}




	//登陆方法
	private void doLogin(){
		try{
			//连接服务器端
			//this.linkInfo.initSocket();/////////////////////////////////////////////////////////////////////////////////////////////

			if(this.clientmain.getSocket()!=null){
				//做一些简单的验证，实际上这里应该有比较复杂的验证，比如敏感字符
				if(jTextFieldLoginUserName.getText()!=null&&!jTextFieldLoginUserName.getText().equals("")&&passwordField.getText()!=null&&!passwordField.getText().equals("")){
					if(in==null){
						in = this.clientmain.getSocket().getInputStream();
					}
					if(out==null){
						out = this.clientmain.getSocket().getOutputStream();
					}
					/*//如果获取来自服务器端信息的线程getServerMsgThread没有启动，则启动getServerMsgThread线程
					if(getServerMsgThread==null){
						getServerMsgThread = new GetServerMsgThread(this.linkInfo);
						new Thread(getServerMsgThread).start();
					}*/
					//登陆过程中，使登陆界面上的登陆按钮不可用
					jButtonLogin.setEnabled(false);
					//在LinkInfo对象中添加当前用户的用户名
					this.clientmain.setMe(jTextFieldLoginUserName.getText());
					//发送登陆请求
					IOUtil.writeShort(Request_Command.LOGIN, out);
					IOUtil.writeString(jTextFieldLoginUserName.getText(), out);
					IOUtil.writeString(passwordField.getText(), out);
				}else{
					JOptionPane.showMessageDialog(null, "用户名或密码为空");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
